/**
 * Created by geqiong on 17/5/25.
 */
var vm = new Vue({
    el:"#app",
    data:{
        limitNumber:3,
        addressList:[],
        currentIndex:0, //当前选中地址的索引
        shippingMethod:1,
        delFlag:false,
        delIndex:0,
        addAddress:false,
        newAddress:{
            "userName":"",
            "streetName":"",
            "postCode":"",
            "tel":"",
            "isDefault":false
        }
    },
    mounted:function(){
        this.$nextTick(function(){
            this.getAddRessList();
        })
    },
    computed:{
        filterAddress:function(){
            return this.addressList.slice(0,this.limitNumber);//返回全新的数组  this.addressList 不变
        }
    },
    methods:{
        getAddRessList:function(){
            let _this = this;
          this.$http.get('data/address.json').then(function(response){
              var res =response.data;
              if(res.status == 0){
                  _this.addressList = res.result;
              }
          })
        },
        more:function(){
            this.limitNumber == 3?
                this.limitNumber=this.addressList.length
                :this.limitNumber=3
        },
        setDefault:function(item){
            let n = this.addressList.indexOf(item);
            this.addressList.forEach(function(address,index){
                if(index == n){
                    address.isDefault = true;
                }else{
                    address.isDefault = false;
                }
            });
        },
        //删除确认弹框
        delConfirm:function(address){
            this.delFlag=true;
            this.delIndex=this.addressList.indexOf(address);
        },
        //确认删除
        delAddress:function(){
            this.addressList.splice(this.delIndex,1);
            this.delFlag=false;
        },
        //保存添加
        saveAdd:function(){
            this.addressList.push(this.newAddress);
            this.addAddress=false;
            this.newAddress={
                "userName":"",
                "streetName":"",
                "postCode":"",
                "tel":"",
                "isDefault":false
            };
        }
    }
});